package com.tangcco.lockwell.controller.admin;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tangcco.lockwell.base.RestResponse;
import com.tangcco.lockwell.context.WebContext;
import com.tangcco.lockwell.domain.DataDictionary;
import com.tangcco.lockwell.service.impl.DataDictionaryServiceImpl;
import com.tangcco.lockwell.service.impl.SubjectServiceImpl;
import com.tangcco.lockwell.viewmodel.admin.dataDictionary.DataDictionaryVM;
import com.tangcco.lockwell.viewmodel.admin.dataDictionary.SubjectAndQuestionTypeVM;
import com.tangcco.lockwell.viewmodel.admin.subject.SubjectVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * author: 宋鑫垚
 * **/
@RestController("DataDictionaryController")
@RequestMapping(value = "/api/admin/dataDictionary")
public class DataDictionaryController {
    @Resource
    private final DataDictionaryServiceImpl dataDictionaryService;

    @Autowired
    WebContext webContext;

    @Resource
    SubjectServiceImpl subjectService;


    public DataDictionaryController(DataDictionaryServiceImpl dataDictionaryService) {
        this.dataDictionaryService = dataDictionaryService;
    }
    //分页查询 （灵活分页查询）
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResponse<PageInfo<DataDictionaryVM>> pageList(@RequestBody DataDictionaryVM model) {
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(model.getPageIndex(), model.getPageSize());

        List<DataDictionaryVM> dataDictionaryVMS = dataDictionaryService.selectDataDictionaryByPage(model);
        PageInfo<DataDictionaryVM> pageInfo = new PageInfo<>(dataDictionaryVMS);
        return RestResponse.ok(pageInfo);
    }
    //添加/修改
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public RestResponse edit(@RequestBody @Valid DataDictionary model) {
        Integer userId = webContext.getCurrentUser().getUserId();
        if (model.getId() == null) {
            model.setCreatedBy(userId);
            dataDictionaryService.insert(model);
        } else {
            model.setModifyBy(userId);
            dataDictionaryService.updateById(model);
        }
        return RestResponse.ok();
    }
    //查看详情
    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public RestResponse<DataDictionary> select(@PathVariable Integer id) {
        DataDictionary dataDictionary = dataDictionaryService.selectById(id);
        return RestResponse.ok(dataDictionary);
    }
    //删除
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public RestResponse delete(@PathVariable Integer id) {
        dataDictionaryService.deleteById(id);
        return RestResponse.ok();
    }

    //查询所有学科和题型
    @RequestMapping(value = "/selectMQ", method = RequestMethod.POST)
    public RestResponse<List<SubjectAndQuestionTypeVM>> selectAll() {
        //要返回的类型
        SubjectAndQuestionTypeVM subjectAndQuestionTypeVM = new SubjectAndQuestionTypeVM();
        //定义一个数据字典模型 并查询所有主要的科目
        List<SubjectVM> subjectVMS = subjectService.selectAllMainSubject();
        //所有主要科目保存的地方
        List<List<Object>> mainSubject = new ArrayList<>();
        //查询所有主要科目并保存
        for (SubjectVM subjectVM : subjectVMS) {
            List<Object> list = new ArrayList<>();
            list.add(subjectVM.getId());
            list.add(subjectVM.getCategoryName());
            List<SubjectVM> subjectVMS1 = subjectService.selectAllSubject(subjectVM.getId());
            List<Object> list1 = new ArrayList<>();
            for (SubjectVM subjectVM1 : subjectVMS1){
                List<Object> list2 = new ArrayList<>();
                list2.add(subjectVM1.getId());
                list2.add(subjectVM1.getCategoryName());
                list1.add(list2);
            }
            list.add(list1);
            mainSubject.add(list);
        }
        //查找所有题目类型
        DataDictionary dataDictionary = new DataDictionary();
        List<List<Object>> AllQuestionType = new ArrayList<>();
        dataDictionary.setCategoryCode("question_type");
        List<DataDictionary> dataDictionaryList = dataDictionaryService.dataDictionarySelective(dataDictionary);
        for (DataDictionary dataDictionary1 : dataDictionaryList) {
            List<Object> list = new ArrayList<>();
            list.add(dataDictionary1.getId());
            list.add(dataDictionary1.getCategoryName());
            list.add(dataDictionary1.getParentId());
            AllQuestionType.add(list);
        }
        subjectAndQuestionTypeVM = new SubjectAndQuestionTypeVM(mainSubject, AllQuestionType);
        return RestResponse.ok(Collections.singletonList(subjectAndQuestionTypeVM));
    }

    /**
     * 根据条件查询学生端菜单树
     * @return
     */
    @RequestMapping(value = "/findTreeByCondition", method = RequestMethod.GET)
    public RestResponse<List<DataDictionaryVM>> findTreeByCondition() {
        List<DataDictionary> parentList = dataDictionaryService.findFistMenuForSubject();
        List<DataDictionaryVM> resultList = BeanUtil.copyToList(parentList,DataDictionaryVM.class);
        if(CollectionUtil.isNotEmpty(resultList)){
            resultList.forEach(item->{
                DataDictionary childSubject = new DataDictionary();
                childSubject.setParentId(item.getId());
                List<DataDictionary> childList = dataDictionaryService.findMenuTreeByCondition(childSubject);
                if(CollectionUtil.isNotEmpty(childList)){
                    item.setChildrenList(BeanUtil.copyToList(childList,DataDictionaryVM.class));
                }else{
                    List<DataDictionaryVM> defaultList = new ArrayList<>();
                    defaultList.add(BeanUtil.copyProperties(item,DataDictionaryVM.class));
                    item.setChildrenList(defaultList);
                }
            });
        }
        return RestResponse.ok(resultList);
    }

    //返回所有的分类编码及其id
    @RequestMapping(value = "/selectAllCode", method = RequestMethod.POST)
    public RestResponse<List<DataDictionary>> selectAllCode() {
        List<DataDictionary> dataDictionaryList = dataDictionaryService.selectAllCode();
        System.out.println(dataDictionaryList.size());
        return RestResponse.ok(dataDictionaryList);
    }
    //获取所有的节点
    @RequestMapping(value = "/selectAllJD", method = RequestMethod.POST)
    public RestResponse<List<DataDictionary>> selectAllJD() {
        List<DataDictionary> dataDictionaries = dataDictionaryService.dataDictionarySelective(null);
        System.out.println(dataDictionaries.size());
        return RestResponse.ok(dataDictionaries);
    }

    //获取所有章节Id 和 父Id
    @RequestMapping(value = "/selectAllChapter", method = RequestMethod.POST)
    public RestResponse<List<DataDictionary>> selectAllChapter() {
        List<DataDictionary> chapter = dataDictionaryService.selectDataDictionaryByCategoryCode("chapter");
        return RestResponse.ok(chapter);
    }
    //根据Id进行查询，并返回父ID
    @RequestMapping(value = "/selectParentId/{id}", method = RequestMethod.POST)
    public RestResponse<Integer> selectParentId(@PathVariable Integer id) {
        DataDictionary dataDictionary = dataDictionaryService.selectById(id);
        if (dataDictionary.getParentId() == null){
            return RestResponse.ok(id);
        }
        return RestResponse.ok(dataDictionary.getParentId());
    }

    //查询所有消息类型
    @RequestMapping(value = "/selectAllInformType", method = RequestMethod.POST)
    public RestResponse<List<DataDictionary>> selectAllMessageType() {
        DataDictionary dataDictionary = new DataDictionary();
        dataDictionary.setCategoryCode("inform_type");
        List<DataDictionary> dataDictionaries = dataDictionaryService.dataDictionarySelective(dataDictionary);
        return RestResponse.ok(dataDictionaries);
    }

}
